<?php
/**
 * Goods
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/2/7
 * Time: 15:55
 */

namespace app\common\model\mysql;

use think\Model;

class Goods extends BaseModel
{
    public function searchTitleAttr($query,$value){
         $query->where('title','like','%'. $value .'%');
    }
    public function searchCreateTimeAttr($query,$value){
        $query->whereBetweenTime('create_time',$value[0],$value[1]);
    }
    public function getLists($likeKeys,$data,$num = 10){
        $order = ['listorder' => 'desc','id' => 'desc'];
        if(!empty($likeKeys)){
            //搜索器
            $res = $this->withSearch($likeKeys,$data);
        }else{
            $res = $this;
        }
        $list = $res->whereIn('status',[0,1])
                     ->order($order)
                     ->paginate($num);
//        echo $res->getLastSql();die;
        return $list;
    }

    /**
     * 获取正常的商品
     * @param $where
     * @param bool $field
     * @param int $limit
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalGoodByCondittion($where, $field = true, $limit = 5){
        $order = ['listorder' => 'desc','id' => 'desc'];

        $where['status'] = config('status.success');

        $result = $this->where($where)
                       ->field($field)
                       ->order($order)
                       ->limit($limit)
                       ->select();
        return $result;
    }
    public function getBigImageAttr($value){
        return request()->domain().$value;
    }
    public function getImageAttr($value){
        return request()->domain().$value;
    }
    public function getCarouselImageAttr($value){
            if(!empty($value)){
                $value = explode(',',$value);
                $value = array_map(function ($v){
                    return request()->domain().$v;
                },$value);
            }
            return $value;
    }

    /**
     * 使用 find_in_set 获取商品信息
     * @param $categoryId
     * @param bool $field
     * @param int $limit
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalGoodsFindInSetCategory($categoryId, $field = true, $limit = 10){
        $order = ['listorder' => 'desc','id' => 'desc'];

        $result = $this->whereFindInSet('category_path_id',$categoryId)
                       ->where('status','=',config('status.success'))
                       ->order($order)
                       ->field($field)
                       ->limit($limit)
                       ->select();
        return $result;
    }

    public function getNormalLists($data, $order,$num = 10, $field = true){
        $res = $this;
        if(isset($data['category_path_id'])){
            $res = $this->whereFindInSet('category_path_id',$data['category_path_id']);
        }
        if(isset($data['keyword'])){
            //搜索器
            $res = $this->withSearch(['title'],['title' => $data['keyword']]);
        }
        $result = $res->where('status',config('status.success'))
                      ->order($order)
                      ->field($field)
                      ->paginate($num);
        return $result;
    }
}

